package com.controller.admin;

import com.entity.Blog;
import com.lucene.BlogIndex;
import com.service.BlogService;
import com.util.DateJsonValueProcessorUtil;
import com.util.ResponseUtil;
import com.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *  管理员博客Controller层
 *  @author Administrator
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {

    @Resource
    private BlogService blogService;

    private BlogIndex blogIndex = new BlogIndex();
    /**
     * 添加或者修改博客信息
     */
    @RequestMapping("/save")
    public String save(Blog blog, HttpServletResponse response)throws Exception{

        int resultTotal=0;
        if(blog.getId()==null){
            resultTotal=blogService.addBlog(blog);
            blogIndex.addIndex(blog);
        }else{
            resultTotal=blogService.updateBlog(blog);
            blogIndex.updateIndex(blog);
        }
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 分页查询博客信息
     */
    @RequestMapping("/list/{id}")
    public String list(@RequestParam(value="offset",required=false)int offset,@RequestParam(value="limit",required=false)Integer limit,Blog blog,HttpServletResponse response,@PathVariable("id") Integer id)throws Exception{
        Map<String,Object> map = new HashMap();
        limit=limit+offset;
        map.put("title", StringUtil.formatLike(blog.getTitle()));
        map.put("size", limit);
        map.put("start", offset);
        map.put("bloggerId", id);
        List<Blog> blogList = blogService.list(map);
        Long total = blogService.getTotal(map);
        //转json
        JSONObject result=new JSONObject();
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessorUtil("yyyy-MM-dd"));
        JSONArray jsonArray= JSONArray.fromObject(blogList, jsonConfig);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 博客批量信息删除
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids",required=false)String ids,HttpServletResponse response)throws Exception{
        String [] idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            blogService.deleteBlog(Integer.parseInt(idsStr[i]));
            blogIndex.deleteIndex(idsStr[i]);
        }
        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 通过id查询博客信息
     * @param id
     */
    @RequestMapping("/findById")
    public String findById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
        Blog blog=blogService.findBlogById(Integer.parseInt(id));
        JSONObject result=JSONObject.fromObject(blog);
        ResponseUtil.write(response, result);
        return null;
    }
}
